package com.xiaoqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.pojo.PageResult;
import com.xiaoqi.pojo.QueryPageBean;
import com.xiaoqi.pojo.Result;

import com.xiaoqi.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author PC
 * creationTime: 2022/11/7 9:52
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Reference
    private UserService userService;


    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(
            @RequestParam(value = "currentPage",defaultValue = "1")Integer currentPage,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "queryString",defaultValue = "")String queryString
            ){
        PageResult pageResult = userService.findAll(new QueryPageBean(currentPage, pageSize, queryString));

        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageResult);
    }

    /**
     * 登录查询用户名
     * @return
     */
    @GetMapping("/login")
    public Result login(){
        //通过SecurityContextHolder获得上下文，认证信息中的用户
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        return new Result(true,MessageConstant.LOGIN_SUCCESS,user.getUsername());
    }
}
